<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <include file="./Public/Admin/header.html" />
    <script type="text/javascript" src="__PUBLIC__js/resource.js?2013-10-15-2"></script>
</head>
<body>
<div id="main">
<div class="container-fluid">
<div class="row-fluid">
    <div class="span12">
        <div class="box">
            <div class="box-title">
                <div class="span10">
                    <h3><i class="icon-edit"></i>编辑图文消息     <small></small></h3>

                </div>
                <div class="span2"><a class="btn" onclick="javascript:window.history.go(-1);">返回</a></div>
            </div>

            <div class="box-content">
                <div class="alert">
                    <strong>温馨提示：</strong><br />
                    1. 详细内容和图文链接地址可以其中填写一项，如果填写的详细内容，素材的链接地址会链接到详细内容页，如果要使图文链接地址生效，则不填写详细内容。如果两个都填写了，则链接地址也会链接到详细内容页。
                </div>
                <form action="{:U('Admin/Wechat/editnew')}" method="POST" class="form-horizontal form-validate " >

                    <div class="control-group1 span12">

                        <div class="controls1" style="">

                            <div id="edit_reply_box" class="edit_reply_box span12" style=" ">

                                <div class="panel">
                                    <div class="image_edit_box">
                                        <div class="editBox">
                                            <div class="leftMsg">
                                                <div class="imgsEdit imgnewsBox" id="imgsEdit">
                                                    <div class="time"><?php echo isset($info)?date('Y-m-d',strtotime($info['addtime'])):date('Y-m-d',time())?></div>
                                                    <div class="img" id="item_<?php echo isset($list)?$list[0]->sortnum:0;?>">
                                                        <div class="coverImg"><?php echo $count>0?"<img src='".$list[0]['detaillogo']."' />":'封面图片';?></div>
                                                        <div class="caption" style=" ">
                                                            <span  class="div_caption" data-link="" data-link-name="">{$list[0]['detailtitle']|default="标题"}</span>

                                                        </div>
                                                        <div class="div_content hide"></div>
                                                        <div class="coverCmd">
                                                            <div class="cmdEdit" onClick="covercmdEdit(this,{$list[0]['sortnum']|default=0});"></div>
                                                        </div>
                                                        <input  name="new[sortnum][0]" type="hidden" value="{$list[0]['sortnum']|default=0}" />
                                                        <input  name="new[detailtitle][0]" type="hidden" value="{$list[0]['detailtitle']}"/>
                                                        <input  name="new[detaillogo][0]" type="hidden" value="{$list[0]['detaillogo']}"/>
                                                        <input name="new[detailintro][0]" type="hidden" value="{$list[0]['detailintro']}"/>
                                                        <input  name="new[detailcontent][0]" type="hidden" value="{$list[0]['detailcontent']}"/>
                                                        <input name="new[linktype][0]" type="hidden" value=""/>
                                                        <input  name="new[detaillink][0]" type="hidden" value="{$list[0]['detaillink']}"/>

                                                    </div>
                                                    <ul class="unstyled">
                                                        <if condition="$count gt 0">
                                                        <for start="1" end="$count">
                                                        <li class="item" id="item_{$list[$i]['sortnum']|default=0}">
                                                            <div class="caption div_caption">{$list[$i]['detailtitle']}</div>
                                                            <div class="itemImg coverImg"><?php echo $count>0?"<img src='".$list[$i]['detaillogo']."' />":'缩略图';?></div>
                                                            <div class="div_content hide"></div>
                                                            <div class="coverCmd">
                                                                <div class="cmdEdit" onClick="covercmdEdit(this,{$list[$i]['sortnum']|default=0});"></div>
                                                                <div class="cmdDel" onClick="covercmdDel(this);"></div>
                                                            </div>
                                                            <input  name="new[sortnum][{$list[$i]['sortnum']|default=1}]" type="hidden" value="{$list[$i]['sortnum']|default=0}" />
                                                            <input  name="new[detailtitle][{$list[$i]['sortnum']|default=1}]" type="hidden" value="{$list[$i]['detailtitle']}"/>
                                                            <input  name="new[detaillogo][{$list[$i]['sortnum']|default=1}]" type="hidden" value="{$list[$i]['detaillogo']}"/>
                                                            <input name="new[detailintro][{$list[$i]['sortnum']|default=1}]" type="hidden" value="{$list[$i]['detailintro']}"/>
                                                            <input  name="new[detailcontent][{$list[$i]['sortnum']|default=1}]" type="hidden" value="{$list[$i]['detailcontent']}"/>
                                                            <input name="new[linktype][{$list[$i]['sortnum']|default=1}]" type="hidden" value=""/>
                                                            <input  name="new[detaillink][{$list[$i]['sortnum']|default=1}]" type="hidden" value="{$list[$i]['detaillink']}"/>
                                                        </li>
                                                        </for>
                                                        <else/>
                                                        <li class="item" id="item_1">
                                                            <div class="caption div_caption" data-link="" data-link-name=""></div>
                                                            <div class="itemImg coverImg">缩略图</div>
                                                            <div class="div_content hide"></div>
                                                            <div class="coverCmd">
                                                                <div class="cmdEdit" onClick="covercmdEdit(this,1);"></div>
                                                                <div class="cmdDel" onClick="covercmdDel(this);"></div>
                                                            </div>
                                                            <input name="new[sortnum][1]" type="hidden" value="1"/>
                                                            <input name="new[detailtitle][1]" type="hidden" value=""/>
                                                            <input name="new[detaillogo][1]" type="hidden" value=""/>
                                                            <input name="new[detailintro][1]" type="hidden" value=""/>
                                                            <input name="new[detailcontent][1]" type="hidden" value=""/>
                                                            <input name="new[linktype][1]" type="hidden" value=""/>
                                                            <input name="new[detaillink][1]" type="hidden" value=""/>
                                                        </li>
                                                        </if>
                                                    </ul>
                                                    <div class="add">
                                                        <i class="icon-plus"></i> 增加一条
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="rightEdit" id="rightEdit" data-index="0">
                                                <div class="rightEditBox">
                                                    <i class="a"></i><i class="b"></i>
                                                    <div class="sp">标题</div>
                                                    <div class="input">
                                                        <input type="text" id="txt_caption" name="txt_caption" class="span12 required" />
                                                    </div>
                                                    <div class="sp">
                                                        封面<span id="imgmsg" class="pull-right" style="font-size: 12px; font-weight: normal;;">大图片建议尺寸：720像素 * 400像素</span>
                                                    </div>
                                                    <div class="input">
                                                        <button type="button" class="btn" onClick="upImage();"><i class=" icon-upload"></i> 上传</button><br />
                                                        <img id="thumb_img" src="" style="max-height:80px;display: none;" />

                                                    </div>
                                                    <div class="sp">摘要</div>
                                                    <div class="input">
                                                        <textarea id="txt_intro" name="txt_intro" class="span12 "  style="height:50px;"></textarea>
                                                    </div>
                                                    <div class="sp">详细内容</div>
                                                    <div class="input">
                                                        <textarea id="txt_content" name="txt_content" class="span12" style=""></textarea>

                                                    </div>

                                                    <div id="res_block" class="">
                                                        <div id="r_link" class=" r-module">
                                                            <div class="input">
                                                                <span class="sp">图文链接地址(url)</span>
                                                                <input type="text" id="source_url" value="" class="span12 " name="source_url" />

                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="edit_reply_box text-center" style="">

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="span12">
                        <input type="hidden" name="action" id="action" value="<?php echo $action?>">
                        <input type="hidden" name="cid" id="cid" value="<?php echo $cid?>">
                        <button id="bsubmit" type="submit" data-loading-text="提交中..." class="btn btn-primary">保存</button>
                        <!--<button type="button" class="btn btn-primary" id="btnSave">保存</button>-->
                        <a href="javascript:godebug()" class="btn hide"></a>
                        <a class="btn" onclick="javascript:window.history.go(-1);">返回</a>

                    </div>

                </form>
            </div>
        </div>
    </div>

</div>
</div>
</div>

<script type="text/javascript" charset="utf-8" src="__PUBLIC__../ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__../ueditor/ueditor.all.js"></script>

<script type="text/javascript">
    //设置编辑器参数
    var param="type=<?php echo isset($utype)?$utype:''?>&comid=<?php echo isset($comid)?$comid:''?>&aid=<?php echo isset($accountid)?$accountid:''?>";
    var options = {
        initialFrameWidth:"100%",
        initialFrameHeight: 260,
        imageUrl:window.UEDITOR_CONFIG.UEDITOR_HOME_URL+"php/imageUp.php?"+param,
        fileUrl:window.UEDITOR_CONFIG.UEDITOR_HOME_URL+"php/fileUp.php?"+param,
        imageManagerUrl:window.UEDITOR_CONFIG.UEDITOR_HOME_URL + "php/imageManager.php?"+param,
        toolbars:[
            [ 'source', '|', 'undo', 'redo', '|',
                'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist',  '|',

                'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
                'indent',
                'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
                'link', 'unlink', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
                'insertimage', 'emotion',  'horizontal','|','help']
        ]
    };
    var ue = UE.getEditor('txt_content', options);
</script>
<script type="text/plain" id="upload_ue" style="display: none;"></script>
<script type="text/javascript">

    //重新实例化一个编辑器，防止在上面的editor编辑器中显示上传的图片或者文件
    var _editor = UE.getEditor('upload_ue',options);
    _editor.ready(function () {
        //设置编辑器不可用
        _editor.setDisabled();
        //隐藏编辑器，因为不会用到这个编辑器实例，所以要隐藏
        _editor.hide();
        //侦听图片上传
        _editor.addListener('beforeInsertImage', function (t, arg) {
            //将地址赋值给相应的input,只去第一张图片的路径
            //$("#thumb").attr("value", arg[0].src);
            //$("#thumb").addClass('hide');
            //图片预览
            //$("#thumb_img").attr("src", arg[0].src);
            //$("#thumb_img").show();
            uploadImgCallBack(arg[0].src);
        })

        //侦听文件上传，取上传文件列表中第一个上传的文件的路径
        _editor.addListener('afterUpfile', function (t, arg) {
            $("#file").attr("value", _editor.options.filePath + arg[0].url);
        })
    });
    //弹出图片上传的对话框
    function upImage() {
        var myImage = _editor.getDialog("insertimage");
        myImage.open();
    }
    //弹出文件上传的对话框
    function upFiles() {
        var myFiles = _editor.getDialog("attachment");
        myFiles.open();
    }
</script>
<script type="text/javascript">

    $(document).ready(function () {


        var resource = Resource.create();
        var ins = Resource.instance['res_block'];
        ins.result = ins.result || {};
        //ins.result.aid = 9118;
    });
</script>

<script type="text/javascript">
    function godebug(){
        var i=$("#rightEdit").attr("data-index");
        //var a=$("input[name='new[sortnum][0]']").val();
        alert(i);
    }
    //取编辑器值 内容
    ue.addListener( "selectionchange", function () {
        var content=ue.getContent();//取值
        //设置值 ue.setContent('')
        //$('#conmsg').html(content);
        var i = $("#rightEdit").attr("data-index");
        if(!ue.hasContents()){
            //content='';
        }
        $("input[name='new[detailcontent]["+i+"]']").val(content);
    })
    //输入框 标题
    $("#txt_caption").keyup(function () {
        var i = $("#rightEdit").attr("data-index");
        $('#item_'+i).find(".div_caption").eq(0).html($(this).val());
        //赋值
        $("input[name='new[detailtitle]["+i+"]']").val($(this).val());
    });
    //输入框 摘要
    $("#txt_intro").keyup(function () {
        var i = $("#rightEdit").attr("data-index");
        //$('#item_'+i).find(".div_content").eq(0).html($(this).val());
        $("input[name='new[detailintro]["+i+"]']").val($(this).val());
    });
    $("#type").change( function() {
        var i = $("#rightEdit").attr("data-index");
        $("input[name='new[linktype]["+i+"]']").val($(this).val());
    });
    //链接
    $('#source_url').keyup(function(){
        var i = $("#rightEdit").attr("data-index");
        $("input[name='new[detaillink]["+i+"]']").val($(this).val());
    })
    //图片
    function uploadImgCallBack(src)
    {
        $("#thumb_img").attr("src", src);
        $("#thumb_img").show();
        var i = $("#rightEdit").attr("data-index");
        var img = "<img src='"+src+"' />";
        $('#item_'+i).find(".coverImg").eq(0).html(img);
        $("input[name='new[detaillogo]["+i+"]']").val(src);
    }
    var action='<?php echo $action;?>';
    var index = <?php echo isset($list)?$list[count($list)-1]['sortnum']:1;?>;

    //添加一条图文
    $("#imgsEdit div[class=add]").click(function () {
        index++;
        var count = $("#imgsEdit ul li[class=item]").length;
        if (count == 9) {
            alert("多图文，最多可添加10条！");
            return;
        }
        var liNode = "<li class=\"item\" id=\"item_"+index+"\"><div class=\"caption div_caption\" data-link=\"\" data-link-name=\"\"></div><div class=\"div_content hide\"></div><div class=\"itemImg coverImg\">缩略图</div><div class=\"coverCmd\"><div class=\"cmdEdit\"  onclick=\"covercmdEdit(this,"+index+");\"></div><div class=\"cmdDel\" onclick=\"covercmdDel(this);\"></div></div>"
                +"<input name=\"new[sortnum]["+index+"]\" type=\"hidden\" value=\""+index+"\"/>"
                +"<input name=\"new[detailtitle]["+index+"]\" type='hidden' value=''/>"
                +"<input name=\"new[detaillogo]["+index+"]\" type='hidden' value='' />"
                +"<input name=\"new[detailintro]["+index+"]\" type='hidden' value=''/>"
                +"<input name=\"new[detailcontent]["+index+"]\" type='hidden' value=''/>"
                +"<input name=\"new[linktype]["+index+"]\" type=\"hidden\" value=\"\"/>"
                +"<input name=\"new[detaillink]["+index+"]\" type='hidden' value=''/>";
        +"</li>";
        $("#imgsEdit ul").append(liNode);
    });
    //编辑选择的图文
    function covercmdEdit(a, t) {
        //var index = 0;
        var top;
        if (t == 0) {
            top = "50px";
            //index = 0;
            $('#imgmsg').html('大图片建议尺寸：720像素 * 400像素封面');
        }
        else {
            $('#imgmsg').html('小图片建议尺寸：400像素 * 400像素');
            //var objItem = $(a).parents("li[class=item]");
            var objItem=$('#item_'+t);
            var i = $("#imgsEdit ul li[class=item]").index(objItem);
            top = 141 + i * 100 +50+ "px";
            //index = i + 1;
        }
        $("#rightEdit").css("paddingTop", top);
        $("#rightEdit").attr("data-index", t);
        var i=$("#rightEdit").attr("data-index");
        //alert(i);
        reLoadImgRedit(t);
    }
    //删除选择的图文
    function covercmdDel(a) {

        var count = $("#imgsEdit ul li[class=item]").length;
        if (count == 1) {
            alert("多图文，至少两条！");
            return;
        }

        if (confirm("你确定删除")) {
            var objItem = $(a).parents("li[class=item]");

            var i = $("#imgsEdit ul li[class=item]").index(objItem);

            var index_now =parseInt($("#rightEdit").attr("data-index"));
            if (i < index_now) {
                $("#rightEdit").attr("data-index", 0);
                $("#rightEdit").css("paddingTop", 0);
                reLoadImgRedit(0);
            }
            objItem.remove();
        }
    }
    //重载右边编辑区
    function reLoadImgRedit(i) {
        $("#rightEdit").attr("data-index", i);

        var t=$("input[name='new[detailtitle]["+i+"]']").val();
        $('#txt_caption').val(t);
        var logo=$("input[name='new[detaillogo]["+i+"]']").val();
        $("#thumb_img").hide();
        if(logo!=''){
            $("#thumb_img").attr("src", logo);
            $("#thumb_img").show();
        }
        var intro=$("input[name='new[detailintro]["+i+"]']").val();
        $('#txt_intro').val(intro);
        var con=$("input[name='new[detailcontent]["+i+"]']").val();
        //alert(con);
        ue.setContent(con);
        var linktype=$("input[name='new[linktype]["+i+"]']").val();
        $('#type').val(linktype);
        //$('#r_link').hide();
        if(linktype!=''){
            //$('#r_link').show();
        }
        var link=$("input[name='new[detaillink]["+i+"]']").val();
        $('#source_url').val(link);

        $('#business_func').val('');
        //$("#txt_caption").focus();
    }


    $("#bsubmit").click(function(e){
        //检查是否添加
        $("input[name^='new[sortnum]']").each(function(){
            i=$(this).val();

            var title=$("input[name='new[detailtitle]["+i+"]']").val();
            if(title==''){
                covercmdEdit(null,i);
                reLoadImgRedit(i);
                alert('标题不能为空!');
                e.preventDefault();
                return false;
            }
            var logo=$("input[name='new[detaillogo]["+i+"]']").val();
            if(logo==''){
                covercmdEdit(null,i);
                reLoadImgRedit(i);
                alert('必须插入一张图片！');
                e.preventDefault();
                return false;
            }
            var con=$("input[name='new[detailcontent]["+i+"]']").val();
            var link=$("input[name='new[detaillink]["+i+"]']").val();
            if(con==''&&link==''){
                covercmdEdit(null,i);
                reLoadImgRedit(i);
                alert('正文或链接必须插入一项！');
                e.preventDefault();
                return false;
            }
        })

    });

    ue.addListener( 'ready', function( editor ) {
        if(action=='edit'){
            covercmdEdit(null,0);
        }
    });

</script>

</body>
</html>
